using Gameplay.PVE.Utils;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.Framework.Managers;
using Yoozoo.Gta.Common;
using Yoozoo.Libs;

namespace Gameplay.PVE.TurnBase
{
    public class HudComponent : IComponent,ITransmit
    {
        //ui
        private BattleUnit unit;
        private GameObject uiObject;
        private Transform bloodBarTrans;
        private Transform bloodBarTempTrans;
        private Text bloodText;


        private GameObject bloodBar;

        private float stateStartTime = 0;

        private float startDiffTime;//开始发现百分比不同的时间
        private float closeDelay = 0.5f;//靠近的延迟时间
        private float tempPercent;//过度血条的百分比
        private float realPercent;//血条实际的百分比

        void IClass.OnReset()
        {
            
        }

        void IComponent.OnInitialize(BattleUnit unit)
        {
            this.unit = unit;
            uiObject = PveResourceManager.GetResource(PveResourceManager.EPveResType.Hud, 1);
            uiObject.transform.SetParent(unit.gameObject.transform);
            float modelScale = 1;
            uiObject.SetActive(true);
            uiObject.transform.localScale = 0.005f * PveUtils.globalRatio * new Vector3(1,1,1);
            uiObject.transform.localPosition = new Vector3(0, 2f * PveUtils.globalRatio * modelScale, 0);
            bloodBar = uiObject.transform.Find("bloodBar").gameObject;
            //bloodBar.SetActive(false);
            var myBloodBarTrans = uiObject.transform.Find("bloodBar/bar");
            var enemyBloodBarTrans = uiObject.transform.Find("bloodBar/barEnemy");
            if (unit.Data.team == 0)
            {
                bloodBarTrans = myBloodBarTrans;
            }
            else
            {
                bloodBarTrans = enemyBloodBarTrans;
            }

            bloodBarTempTrans = uiObject.transform.Find("bloodBar/barTemp");
            
            myBloodBarTrans.SetActive(unit.Data.team == 0);
            enemyBloodBarTrans.SetActive(unit.Data.team != 0);
            bloodBarTrans.localScale = new Vector3(1,1,1);
            bloodBarTempTrans.localScale = new Vector3(1, 1, 1);
            bloodText = uiObject.transform.Find("bloodBar/bloodText").GetComponent<Text>();
            tempPercent = 1;
            realPercent = 1;
            bloodBar.SetActive(false);
        }

        void IComponent.OnUpdate()
        {
            if (unit.Data.isDead)
            {
                return;
            }

            if (startDiffTime == 0)
            {
                if (realPercent < tempPercent)
                {
                    startDiffTime = TimeManager.logicTime;
                }
                else if (realPercent > tempPercent)
                {
                    tempPercent = realPercent;
                }
            }
            else if (TimeManager.logicTime - startDiffTime > closeDelay)
            {
                float diff = 1 * TimeManager.LogicDeltaTime;
                if (tempPercent - realPercent > diff)
                {
                    tempPercent -= diff;
                }
                else
                {
                    tempPercent = realPercent;
                    startDiffTime = 0;
                }
                bloodBarTempTrans.localScale = new Vector3(tempPercent,1,1);
            }
            
            uiObject.transform.forward = PveCameraManager.Instance.cameraTrans.forward;
        }
        

        void IComponent.OnBirth()
        {
            
        }

        void IComponent.OnDead()
        {
            PveResourceManager.RecoverResource(uiObject,PveResourceManager.EPveResType.Hud,1);
            bloodText = null;
            uiObject = null;
        }
        
        
        void ITransmit.OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (unit.Data.isDead)
            {
                return;
            }
            if (type == ETransmitType.UpdateHp)
            {
                float percent = (float) unit.Data.CurrentHp / unit.Data.TotalHp;
                if (percent < 1)
                {
                    bloodBar.SetActive(true);
                }

                if (percent < 0)
                {
                    percent = 0;
                }
                bloodBarTrans.localScale = new Vector3(percent,1,1);
                if (unit.Data.team == 0)
                {
                    ResidentHandler.Inst.GetFunction("pveManager.TbsUpdateHeroHp").Action(unit.Data.id,percent);
                }

                realPercent = percent;
                bloodText.text = unit.Data.CurrentHp.ToString();
            }
            else if (type == ETransmitType.RoundStart)
            {
                if (!unit.Data.isDead)
                {
                    bloodBar.SetActive(true);
                }
            }
        }
    }
}